﻿using Factory.Customers.DTO;
using Factory.Cutomers;
using Factory.MasterData.Materials.Dto;
using Factory.MesEntity.CustomerDatas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Factory.Customers
{
    public class CustomerDataAppService:ApplicationService, ICustomerDataAppService
    {
        private readonly IRepository<Factory.MesEntity.CustomerDatas.CustomerData,int> _repository;

        public CustomerDataAppService(IRepository<Factory.MesEntity.CustomerDatas.CustomerData,int> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 获取客户数据列表
        /// </summary>
        /// <param name="customerName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<PageResult> GetCustomerDataList(string customerName, int pageIndex = 1, int pageSize = 10)
        {
            var list = await _repository.GetListAsync();
            list = list.WhereIf(!string.IsNullOrEmpty(customerName), x => x.CustomerName.Contains(customerName)).ToList();
            PageResult pageResult = new PageResult();
            pageResult.RowCount = list.Count();
            pageResult.PageCount = (int)Math.Ceiling(pageResult.RowCount * 1.0 / pageSize);
            pageResult.Queryable = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageResult;
        }

        /// <summary>
        /// 添加客户
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public int CreateAsync(CustomerData customer)
        {

            // 异步获取同名单位的记录
            var customerDatalist = _repository.GetListAsync(x => x.CustomerName == customer.CustomerName).Result;

            // 检查是否已存在该单位名称
            if (customerDatalist.Count() > 0)
            {
                return -100;  // 表示重复，或者抛出异常等
            }

            var customerlist = _repository.InsertAsync(new CustomerData
            {
                CustomerName = customer.CustomerName,
                CustomerCode = customer.CustomerCode,
          
                ContactTel = customer.ContactTel,
                CustomerNickName = customer.CustomerNickName,
                CustomerTel = customer.CustomerTel,
                CustomerType = customer.CustomerType,
                Contact=customer.Contact,
                IsNot=customer.IsNot,
            });
            return 1;

      
         }

        /// <summary>
        /// 删除客户数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> DeleteAsync(int id)
        {
            var customerlist = await _repository.FindAsync(x => x.Id == id);
            if (customerlist != null)
            {
                await _repository.DeleteAsync(customerlist);
                return 1;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 获取客户数据详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CustomerData GetCustomerById(int id)
        {
            var customerlist = _repository.FindAsync(id).Result;
            return customerlist;
        }

        /// <summary>
        /// 更新客户数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdateAsync(CustomerDataUpdateDto input)
        {
            var customerlist = await _repository.FindAsync(x => x.Id == input.Id);

            if (customerlist == null)
            {
                // 客户未找到，可以根据需要抛出异常或返回特定值
                return 0;
            }

            customerlist.CustomerCode = input.CustomerCode;
            customerlist.CustomerName = input.CustomerName;
            customerlist.CustomerNickName = input.CustomerNickName;
            customerlist.CustomerTel = input.CustomerTel;
            customerlist.CustomerType = input.CustomerType;
            customerlist.Contact = input.Contact;
            customerlist.ContactTel = input.ContactTel;
            customerlist.IsNot = input.IsNot;

            await _repository.UpdateAsync(customerlist);
            return 1; // 或者返回其他表示成功的值
        }

    }


}
